from apps.common.base.views import *
from apps.merchant.item.serializers import *
from apps.merchant.item.dao import ItemDao
from apps.merchant.item.filters import ItemFilter

def getItemAll(self):
    _list = ItemDao().findAll().select_related('cate', 'activity', 'genera', 'shop').prefetch_related('item_sku', 'tags')
    return _list

class BaseItemViewSet(BaseViewSet):
    serializer_class = ItemModelSerializer
    filter_class = ItemFilter
    DAO_CLASS = ItemDao
    @action(methods=['post'], detail=False, authentication_classes = [TokenAuthentication], permission_classes = [IsAuthenticated])
    def getItemById(self, request):
        params = getRequestParams(self.request)
        obj_id = params.get('obj_id')
        if not obj_id:
            return Response(response_message('obj_id参数必填'))
        obj = self.DAO_CLASS().findOneByObjId(obj_id)
        model = self.serializer_class(obj, context={'request':request}).data
        return Response(response_success(model))